/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.taskmanager.tasks;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;

import com.l2jserver.Config;
import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
import com.l2jserver.gameserver.taskmanager.Task;
import com.l2jserver.gameserver.taskmanager.TaskManager;
import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;
import com.l2jserver.gameserver.taskmanager.TaskTypes;

/**
 * @author Kimera
 */
public class TaskHappyHourStart extends Task
{
	private String param1, param2, param3;
	
	private static final String NAME = "happy_hour_start";
	
	@Override
	public String getName()
	{
		return NAME;
	}
	
	@Override
	public void onTimeElapsed(ExecutedTask task)
	{
		OldConfig.values();
		Date d = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		
		final int day = c.get(Calendar.DAY_OF_WEEK);
		String todayIs = ""; // Dia de hoy
		String[] params = null; // Parametros del evento
		
		// File
		String lineRead = "";
		
		switch (day)
		{
			case 1:
				todayIs = "D";
				break;
			case 2:
				todayIs = "L";
				break;
			case 3:
				todayIs = "M";
				break;
			case 4:
				todayIs = "W";
				break;
			case 5:
				todayIs = "J";
				break;
			case 6:
				todayIs = "V";
			case 7:
				todayIs = "S";
				break;
			default: // Nunca deberia ocurrir
			{
				todayIs = "S";
				_log.info("Algo raro ha ocurrido con las fechas en TaskHappyHourStart: " + day);
			}
		}
		
		if (Config.DEBUG)
		{
			_log.info("Hoy es " + todayIs);
		}
		params = task.getParams();
		if (Config.DEBUG)
		{
			_log.info("Parametros: " + params[0] + " " + params[1] + " " + params[2]);
		}
		
		if (todayIs.equals(params[2].split(" ")[0]))
		{
			_log.info("Evento happyHour iniciado.");
			File f = new File("./config/happyHour.properties");
			try (FileReader fr = new FileReader(f);
				BufferedReader br = new BufferedReader(fr);)
			{
				while ((lineRead = br.readLine()) != null)
				{
					if (lineRead.split(" ")[0].contains("NAME"))
					{
						if (lineRead.split(" ")[1].equals(params[2].split(" ")[1]))
						{
							_log.info("Cargando y aplicando configuracion evento HappyHour.");
							Config.RATE_XP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_SP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_PARTY_XP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_PARTY_SP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_DROP_ITEMS = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_DROP_ITEMS_BY_RAID = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_DROP_SPOIL = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_DROP_MANOR = Integer.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_DROP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_XP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_SP = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_POTION = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_SCROLL = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_RECIPE = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.RATE_QUEST_REWARD_MATERIAL = Float.valueOf((br.readLine()).split(" ")[1]);
							Config.ENCHANT_CHANCE = Double.valueOf((br.readLine()).split(" ")[1]);
							
							// Y de esta ingeniosa forma se actualiza la informacion del Community
							_log.info(" RATE_XP: " + Config.RATE_XP);
							_log.info(" RATE_SP: " + Config.RATE_SP);
							_log.info(" RATE_PARTY_XP: " + Config.RATE_PARTY_XP);
							_log.info(" RATE_PARTY_SP: " + Config.RATE_PARTY_SP);
							_log.info(" RATE_DROP_ITEMS: " + Config.RATE_DROP_ITEMS);
							_log.info(" RATE_DROP_ITEMS_BY_RAID: " + Config.RATE_DROP_ITEMS_BY_RAID);
							_log.info(" RATE_DROP_SPOIL: " + Config.RATE_DROP_SPOIL);
							_log.info(" RATE_DROP_MANOR: " + Config.RATE_DROP_MANOR);
							_log.info(" RATE_QUEST_DROP: " + Config.RATE_QUEST_DROP);
							_log.info(" RATE_QUEST_REWARD_XP: " + Config.RATE_QUEST_REWARD_XP);
							_log.info(" RATE_QUEST_REWARD_SP: " + Config.RATE_QUEST_REWARD_SP);
							_log.info(" RATE_QUEST_REWARD_POTION: " + Config.RATE_QUEST_REWARD_POTION);
							_log.info(" RATE_QUEST_REWARD_SCROLL: " + Config.RATE_QUEST_REWARD_SCROLL);
							_log.info(" RATE_QUEST_REWARD_RECIPE: " + Config.RATE_QUEST_REWARD_RECIPE);
							_log.info(" RATE_QUEST_REWARD_RECIPE: " + Config.RATE_QUEST_REWARD_RECIPE);
							_log.info(" RATE_QUEST_REWARD_MATERIAL: " + Config.RATE_QUEST_REWARD_MATERIAL);
							_log.info(" ENCHANT_CHANCE: " + Config.ENCHANT_CHANCE);
							
							for (L2PcInstance pj : L2World.getInstance().getAllPlayersArray())
							{
								pj.sendPacket(new ExShowScreenMessage("Evento Happyhour iniciado!", 1000));
							}
							
							break;
						}
					}
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void initializate()
	{
		super.initializate();
		_log.info("*** Happyhour (Start time) *** Inicializada");
		String QUERY = "SELECT param1,param2,param3 from global_tasks WHERE task=\"happy_hour_start\"";
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			Statement st = con.createStatement();
			ResultSet executeQuery = st.executeQuery(QUERY))
		{
			while (executeQuery.next())
			{
				param1 = executeQuery.getString("param1");
				param2 = executeQuery.getString("param2");
				param3 = executeQuery.getString("param3"); // Donde reside el dia de la programacion semanal
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		if ((param1 != null) && (param2 != null))
		{
			TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, param1, param2, param3, 0L);
		}
		
		_log.info("Se ha anadido: " + NAME + " " + param1 + " " + param2 + " " + param3);
	}
	
	// TODO: Deberia crearse una clase enumeracion en su propio fichero para reutilizar codigo.
}

enum OldConfig
{
	RATE_XP(Config.RATE_XP),
	RATE_SP(Config.RATE_SP),
	RATE_PARTY_XP(Config.RATE_PARTY_XP),
	RATE_PARTY_SP(Config.RATE_PARTY_SP),
	RATE_DROP_ITEMS(Config.RATE_DROP_ITEMS),
	RATE_DROP_ITEMS_BY_RAID(Config.RATE_DROP_ITEMS_BY_RAID),
	RATE_DROP_SPOIL(Config.RATE_DROP_SPOIL),
	RATE_DROP_MANOR(Config.RATE_DROP_MANOR),
	RATE_QUEST_DROP(Config.RATE_QUEST_DROP),
	RATE_QUEST_REWARD_XP(Config.RATE_QUEST_REWARD_XP),
	RATE_QUEST_REWARD_SP(Config.RATE_QUEST_REWARD_SP),
	RATE_QUEST_REWARD_ADENA(Config.RATE_QUEST_REWARD_SP),
	RATE_QUEST_REWARD_POTION(Config.RATE_QUEST_REWARD_POTION),
	RATE_QUEST_REWARD_SCROLL(Config.RATE_QUEST_REWARD_SCROLL),
	RATE_QUEST_REWARD_RECIPE(Config.RATE_QUEST_REWARD_RECIPE),
	RATE_QUEST_REWARD_MATERIAL(Config.RATE_QUEST_REWARD_MATERIAL),
	ENCHANT_CHANCE(Config.ENCHANT_CHANCE);
	
	float rateFloat;
	int rateInt;
	double rateDouble;
	
	OldConfig(float f)
	{
		rateFloat = f;
	}
	
	OldConfig(int i)
	{
		rateInt = i;
	}
	
	OldConfig(double d)
	{
		rateDouble = d;
	}
	
	float getFloatRate()
	{
		return rateFloat;
	}
	
	int getIntegerRate()
	{
		return rateInt;
	}
	
	double getDoubleRate()
	{
		return rateDouble;
	}
}